from dbmodels.faction import FactionModel

import webapp2

class GeneratedCssHandler(webapp2.RequestHandler):

    def get(self):
        images = []
        factions = FactionModel.all()
        for faction in factions:
            if faction.icon is not None:
                images.append("div.%s { background-image: url(data:image/png;base64,%s); }\n" %
                              (faction.name, faction.icon)
                )

        responseBody = "".join(images)

        self.response.headers['Content-Type'] = 'text/css'
        self.response.headers['Content-Length'] = len(responseBody)
        self.response.write(responseBody)

app = webapp2.WSGIApplication([
    ('/css/generated.css', GeneratedCssHandler)
    ], debug=True)